#pragma once
#include "db.hpp"
#include <cctype>
#include <algorithm>

using namespace std;


void reg(const string& in_json, string& out_json)
{
    // 反序列化
    Json::Value in_value;
    Json_util::unserializa(in_json, in_value);
    // 提取数据
    string username = in_value["username"].asString();
    string password = in_value["password"].asString();
    string phone = in_value["phone"].asString();
    string email = in_value["email"].asString();
    string address = in_value["address"].asString();

    Json::Value out_value;
    // 解析用户名
    if(username.size() > 20)
    {
        out_value["reason"] = "用户名长度不合法";
        Json_util::serializa(out_value, out_json);
        return;
    }
    else if(isdigit(username[0]))
    {
        out_value["reason"] = "用户名不能以数字开头";
        Json_util::serializa(out_value, out_json);
        return;
    }
    for(auto &e : username)
    {
        if((e >= '0' && e <= '9') || (e >= 'a' && e <= 'z') || (e >= 'A' && e <= 'Z'))
        {

        }
        else 
        {
            out_value["reason"] = "用户名格式错误";
            Json_util::serializa(out_value, out_json);
            return;
        }
    }
    // 解析密码
    if(password.size() < 6 || password.size() > 10)
    {
        out_value["reason"] = "密码长度不合法";
        Json_util::serializa(out_value, out_json);
        return;
    }
    for(auto &e : password)
    {
        if((e >= '0' && e <= '9') || (e >= 'a' && e <= 'z') || (e >= 'A' && e <= 'Z'))
        {

        }
        else 
        {
            out_value["reason"] = "密码格式错误";
            Json_util::serializa(out_value, out_json);
            return;
        }
    }
    // 解析手机号
    if(phone.size() != 11)
    {
        out_value["reason"] = "手机号长度不合法";
        Json_util::serializa(out_value, out_json);
        return;
    }
    for(auto &e : phone)
    {
        if(!isdigit(e))
        {
            out_value["reason"] = "手机号格式错误";
            Json_util::serializa(out_value, out_json);
            return;
        }
    }
    // 解析邮箱
    if(email.size() < 10 || email.size() > 20)
    {
        out_value["reason"] = "邮箱长度不合法";
        Json_util::serializa(out_value, out_json);
        return;
    }
    bool r = false;
    for(auto &e : email)
    {
        if(e == '@')
        {
            r = true;
            break;
        }
    }
    if(r == false)
    {
        out_value["reason"] = "邮箱格式错误";
        Json_util::serializa(out_value, out_json);
        return;
    }
    string sep = email.substr(email.size()-4);
    if(sep != ".com")
    {
        out_value["reason"] = "邮箱格式错误";
        Json_util::serializa(out_value, out_json);
        return;
    }

    // 注册
    user_table ut(g_host, g_user, g_passwd, g_db, g_port);
    bool ret = ut.insert(username, password, phone, email, address);
    
    // 返回信息
    if(!ret)
    {
        out_value["reason"] = "注册失败...";
        Json_util::serializa(out_value, out_json);
        return;
    }
    out_value["reason"] = "注册成功...";
    Json_util::serializa(out_value, out_json);
    return;
}